package com.zhaoxi.project.md.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.zhaoxi.common.utils.DateUtils;
import com.zhaoxi.project.system.service.impl.AutoCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.zhaoxi.project.md.mapper.MdGoodsMapper;
import com.zhaoxi.project.md.domain.MdGoods;

/**
 * 物品Service业务层处理
 *
 * @author zhaoxi
 * @date 2023-07-22
 */
@Service
public class MdGoodsServiceImpl {
    @Autowired
    private MdGoodsMapper mdGoodsMapper;

    @Autowired
    private AutoCodeUtil autoCodeUtil;
    /**
     * 查询物品
     *
     * @param parentCode 物品主键
     * @return 物品
     */

    public MdGoods selectMdGoodsByParentCode(String parentCode) {
        return mdGoodsMapper.selectMdGoodsByParentCode(parentCode);
    }

    /**
     * 查询物品列表
     *
     * @param mdGoods 物品
     * @return 物品
     */

    public List<MdGoods> selectMdGoodsList(MdGoods mdGoods) {
        return mdGoodsMapper.selectMdGoodsList(mdGoods);
    }

    /**
     * 新增物品
     *
     * @param mdGoods 物品
     * @return 结果
     */
    @Transactional

    public int insertMdGoods(MdGoods mdGoods) {
        String goodsCode = autoCodeUtil.genSerialCode("GOODS_CODE",null);
        mdGoods.setParentCode(goodsCode);
//        int rows = mdGoodsMapper.insertMdGoods(mdGoods);
//        insertMdGoodsLine(mdGoods);
        return mdGoodsMapper.insertMdGoods(mdGoods);
    }

    /**
     * 修改物品
     *
     * @param mdGoods 物品
     * @return 结果
     */
    @Transactional

    public int updateMdGoods(MdGoods mdGoods) {
        mdGoods.setUpdateTime(DateUtils.getNowDate());
        mdGoodsMapper.deleteMdGoodsLineByParentCode(mdGoods.getParentCode());
//        insertMdGoodsLine(mdGoods);
        return mdGoodsMapper.updateMdGoods(mdGoods);
    }

    /**
     * 批量删除物品
     *
     * @param parentCodes 需要删除的物品主键
     * @return 结果
     */
    @Transactional

    public int deleteMdGoodsByParentCodes(String[] parentCodes) {
        mdGoodsMapper.deleteMdGoodsLineByParentCodes(parentCodes);
        return mdGoodsMapper.deleteMdGoodsByParentCodes(parentCodes);
    }

    /**
     * 删除物品信息
     *
     * @param parentCode 物品主键
     * @return 结果
     */
    @Transactional

    public int deleteMdGoodsByParentCode(String parentCode) {
        mdGoodsMapper.deleteMdGoodsLineByParentCode(parentCode);
        return mdGoodsMapper.deleteMdGoodsByParentCode(parentCode);
    }

    public List<MdGoods> selectMdGoodsLinesList(String parentCode) {
        return mdGoodsMapper.getMdGoodsLinesList(parentCode);
    }

    public int deleteLineByCodes(String code) {
        return mdGoodsMapper.deleteMdGoodsLineByCode(code);
    }

    public int saveMdGoodsLines(MdGoods mdGoods) {
        return mdGoodsMapper.insertMdGoodsLine(mdGoods);
    }

//    /**
//     * 新增物料产品信息
//     *
//     * @param mdGoods 物品对象
//     */
//    public void insertMdGoodsLine(MdGoods mdGoods) {
//        List<MdGoodsLine> mdGoodsLineList = mdGoods.getMdGoodsLineList();
//        String parentCode = mdGoods.getParentCode();
//        if (StringUtils.isNotNull(mdGoodsLineList)) {
//            List<MdGoodsLine> list = new ArrayList<MdGoodsLine>();
//            for (MdGoodsLine mdGoodsLine : mdGoodsLineList) {
//                mdGoodsLine.setParentCode(parentCode);
//                list.add(mdGoodsLine);
//            }
//            if (list.size() > 0) {
//                mdGoodsMapper.batchMdGoodsLine(list);
//            }
//        }
//    }
}
